home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Numerical / Test / test_items < prev   
Encoding:
Text File  |  2000-06-23  |  11.6 KB  |  757 lines

  1. #!/usr/bin/env python
  2. import sys
  3. from Numeric import *
  4.  
  5. #Creation from a nested sequence
  6. array([1,2,3])
  7. >array([1, 2, 3], 'l')
  8.  
  9. array([1,2.3,4])
  10. >array([1.0, 2.3, 4.0], 'd')
  11.  
  12. array([1,2j,3.])
  13. >array([(1+0j), 2j, (3+0j)], 'D')
  14.  
  15. array([1,2,3], Float32)
  16. >array([1.0, 2.0, 3.0], 'f')
  17.  
  18. array([[1,2],[11,12]])
  19. >array([[1, 2], [11, 12]], 'l')
  20.  
  21. array([[],[]]).shape
  22. >(2, 0)
  23.  
  24. array([])
  25. >array([], 'l')
  26.  
  27. #These are not just convenience functions
  28. zeros( (2,3) )
  29. >array([[0, 0, 0], [0, 0, 0]], 'l')
  30.  
  31. ones( (2,3), Int16)
  32. >array([[1, 1, 1], [1, 1, 1]], 's')
  33.  
  34. #arrayrange now
  35. arrayrange(7, step=2)
  36. >array([0, 2, 4, 6], 'l')
  37.  
  38. arrayrange(0, 1.5, .5)
  39. >array([0.0, 0.5, 1.0], 'd')
  40.  
  41. #Creation from String Data
  42. fromstring("\001\002\003", 'b')
  43. >array([1, 2, 3], 'b')
  44.  
  45.  
  46. #Structural array operations
  47. m = array([[1,2,3], [11,12,13]])
  48. a = arange(6)
  49.  
  50. a[2:-1]
  51. >array([2, 3, 4], 'l')
  52.  
  53. a[::-1]
  54. >array([5, 4, 3, 2, 1, 0], 'l')
  55.  
  56.  
  57. m[0,2]
  58. >3
  59.  
  60. m[...,1]
  61. >array([2, 12], 'l')
  62.  
  63. #take
  64.  
  65. take(a, (3,2,1,2,3))
  66. >array([3, 2, 1, 2, 3], 'l')
  67.  
  68. take(m, (2,2,1,1), -1)
  69. >array([[3, 3, 2, 2], [13, 13, 12, 12]], 'l')
  70.  
  71. #Rearranging array elements
  72.  
  73. reshape(m, (-1,2))
  74. >array([[1, 2], [3, 11], [12, 13]], 'l')
  75.  
  76. reshape(a, (2,3))
  77. >array([[0, 1, 2], [3, 4, 5]], 'l')
  78.  
  79. reshape(a, (4,-1))
  80. >exceptions.ValueError: total size of new array must be unchanged
  81.  
  82. reshape(a, (5,6))
  83. >exceptions.ValueError: total size of new array must be unchanged
  84.  
  85. ravel(m)
  86. >array([1, 2, 3, 11, 12, 13], 'l')
  87.  
  88. a[..., NewAxis]
  89. >array([[0], [1], [2], [3], [4], [5]], 'l')
  90.  
  91. transpose(m, (1,0))
  92. >array([[1, 11], [2, 12], [3, 13]], 'l')
  93.  
  94. #Replicating and combining array elements
  95.  
  96. repeat(a, (0,1,2,3,0,0))
  97. >array([1, 2, 2, 3, 3, 3], 'l')
  98.  
  99. repeat(a, (0,)*6)
  100. >array([], 'l')
  101.  
  102. repeat(m, (0,2,1), -1)
  103. >array([[2, 2, 3], [12, 12, 13]], 'l')
  104.  
  105. #al = [0,1,2,3,4,5]
  106. concatenate( (a[:3], a[3:]) )
  107. >array([0, 1, 2, 3, 4, 5], 'l')
  108.  
  109. concatenate( (m, m) )
  110. >array([[1, 2, 3], [11, 12, 13], [1, 2, 3], [11, 12, 13]], 'l')
  111.  
  112. concatenate( (m, m), axis=1)
  113. >array([[1, 2, 3, 1, 2, 3], [11, 12, 13, 11, 12, 13]], 'l')
  114.  
  115. #arithmetic and logic operators
  116. x = array([1,2], 'l')
  117. y = zeros((2,), 'l')
  118.  
  119. x+x
  120. >array([2, 4], 'l')
  121.  
  122. add(x,x)
  123. >array([2, 4], 'l')
  124.  
  125. add(x,x,y)
  126. >array([2, 4], 'l')
  127.  
  128. y
  129. >array([2, 4], 'l')
  130.  
  131. x-x
  132. >array([0, 0], 'l')
  133.  
  134. subtract(x,x)
  135. >array([0, 0], 'l')
  136.  
  137. x*x
  138. >array([1, 4], 'l')
  139.  
  140. multiply(x, x)
  141. >array([1, 4], 'l')
  142.  
  143. x/x
  144. >array([1, 1], 'l')
  145.  
  146. divide(x,x)
  147. >array([1, 1], 'l')
  148.  
  149. x**x
  150. >array([1, 4], 'l')
  151.  
  152. power(x,x)
  153. >array([1, 4], 'l')
  154.  
  155. x%x
  156. >array([0, 0], 'l')
  157.  
  158. remainder(x,x)
  159. >array([0, 0], 'l')
  160.  
  161. x<<1
  162. >array([2, 4], 'l')
  163.  
  164. left_shift(x, 1)
  165. >array([2, 4], 'l')
  166.  
  167. x>>1
  168. >array([0, 1], 'l')
  169.  
  170. right_shift(x, 1)
  171. >array([0, 1], 'l')
  172.  
  173. x & 2
  174. >array([0, 2], 'l')
  175.  
  176. bitwise_and(x, 2)
  177. >array([0, 2], 'l')
  178.  
  179. x | 1
  180. >array([1, 3], 'l')
  181.  
  182. bitwise_or(x, 1)
  183. >array([1, 3], 'l')
  184.  
  185. x ^ 2
  186. >array([3, 0], 'l')
  187.  
  188. bitwise_xor(x, 2)
  189. >array([3, 0], 'l')
  190.  
  191. #Non-elementwise operations
  192. add.reduce(a)
  193. >15
  194.  
  195. add.reduce(m, 0)
  196. >array([12, 14, 16], 'l')
  197.  
  198. add.reduce(m, -1)
  199. >array([6, 36], 'l')
  200.  
  201. add.reduce([1])
  202. >1
  203.  
  204. add.reduce([])
  205. >0
  206.  
  207. multiply.reduce([])
  208. >1
  209.  
  210. add.outer(a[:3], a[:3])
  211. >array([[0, 1, 2], [1, 2, 3], [2, 3, 4]], 'l')
  212.  
  213. dot(a,a)
  214. >55
  215.  
  216. #conditional selection
  217.  
  218. choose(a, (5,4,3,2,1,0))
  219. >array([5, 4, 3, 2, 1, 0], 'l')
  220.  
  221. choose([[1,0], [0,1]], (66, [(1,2),(11,12)]))
  222. >array([[1, 66], [66, 12]], 'l')
  223.  
  224. #sorting
  225.  
  226. s = (3,2,5,1,4,0)
  227. sm = [s, array(s)[::-1]]
  228. se = array(s)[0:0]
  229.  
  230. sort( s )
  231. >array([0, 1, 2, 3, 4, 5], 'l')
  232.  
  233. sort(se)
  234. >zeros((0,), 'l')
  235.  
  236. argsort( s )
  237. >array([5, 3, 1, 0, 4, 2], 'l')
  238.  
  239. argsort( se )
  240. >zeros((0,), 'l')
  241.  
  242. sort(sm, axis=-1)
  243. >array([[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]], 'l')
  244.  
  245. sort(sm, axis=0)
  246. >array([[0, 2, 1, 1, 2, 0], [3, 4, 5, 5, 4, 3]], 'l')
  247.  
  248. searchsorted( arange(10), (5, 2))
  249. >array([5, 2], 'l')
  250.  
  251. #argmax/min
  252.  
  253. argmax( s )
  254. >2
  255.  
  256. argmax( sm, axis=-1 )
  257. >array([2, 3], 'l')
  258.  
  259. argmax(sm, axis=0)
  260. >array([0, 1, 0, 1, 0, 1], 'l')
  261.  
  262. argmin(sm, axis=-1)
  263. >array([5, 0], 'l')
  264.  
  265. #Implementation methods
  266.  
  267. array([3,4], Int32).itemsize()
  268. >4
  269.  
  270. a.astype('i').byteswapped()
  271. >array([0, 16777216, 33554432, 50331648, 67108864, 83886080], 'i')
  272.  
  273. a.typecode()
  274. >l
  275.  
  276. a.iscontiguous()
  277. >1
  278.  
  279. a.astype('d')
  280. >array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0], 'd')
  281.  
  282. u = array([0,1,2], Int8)
  283. repr(u.tostring())
  284. >'\000\001\002'
  285.  
  286. c = array([0,1,1j])
  287. c.real
  288. >array([0.0, 1.0, 0.0], 'd')
  289.  
  290. c.imag
  291. >array([0.0, 0.0, 1.0], 'd')
  292.  
  293. #init = "from multiarray import *\nm = array([(5,2,6,9,3,4,1,0), (8,0,1,3,6,2,0,4)])"
  294. #sorts = [("sort(m)", array([(0,1,2,3,4,5,6,9),(0,0,1,2,3,4,6,8)], 'l') ),
  295. #    ("argmax(m)[1]", array([3,0], 'l') ),
  296. #    ("argmin(m)[1]", array([7,1], 'l') ),
  297. #    ]
  298. #do_eval(sorts, initialize = init)
  299.  
  300.  
  301.  
  302. #sort and argsort need axis
  303.  
  304. a = arange(6)
  305.  
  306. #from Guido
  307. a[...]
  308. >array([0, 1, 2, 3, 4, 5], 'l')
  309.  
  310. array([None])
  311. >array([None], 'O')
  312.  
  313. zeros( (2,), 'O')
  314. >array([0, 0], 'O')
  315.  
  316. #from Janne
  317. b = array(a)
  318. b[0] = 66
  319. a[0]
  320. >0
  321.  
  322. zeros((-5,))
  323. >exceptions.ValueError: negative dimensions are not allowed
  324.  
  325. arange(5,3,2)
  326. >array([], 'l')
  327.  
  328. minimum.reduce()
  329. >exceptions.TypeError: function requires at least 1 argument; 0 given
  330.  
  331. minimum.reduceat()
  332. >exceptions.TypeError: function requires exactly 2 arguments; 0 given
  333.  
  334. b = array([[1, 2, 3, 4], [5, 6, 7, 8]]*2)
  335.  
  336. diagonal(b)
  337. >array([1, 6, 3, 8], 'l')
  338.  
  339. diagonal(b, -1)
  340. >array([5, 2, 7], 'l')
  341.  
  342. c = array([b,b])
  343.  
  344. diagonal(c, 1)
  345. >array([[2, 7, 4], [2, 7, 4]], 'l')
  346.  
  347. #From Carlos
  348. b[1:1].shape
  349. >(0, 4)
  350.  
  351. b[1:1,:].shape
  352. >(0, 4)
  353.  
  354. b[10:]
  355. >zeros((0,4))
  356.  
  357. b[10:, :]
  358. >zeros((0,4))
  359.  
  360. b[2:10]
  361. >array([[1, 2, 3, 4], [5, 6, 7, 8]], 'l')
  362.  
  363. b[2:10, ...]
  364. >array([[1, 2, 3, 4], [5, 6, 7, 8]], 'l')
  365.  
  366. 1/array(0)
  367. >exceptions.ZeroDivisionError: divide by zero
  368.  
  369. #1/array(0.)
  370. #Machine dependent value, just hope there's no system crash on this
  371. #>1.#INF
  372.  
  373. #Tim Hochberg
  374.  
  375. choose((0,1,2),([1,1,1],[2,2,2]))
  376. >exceptions.ValueError: invalid entry in choice array
  377.  
  378. where((0,1,2), [1,1,1],[2,2,2])
  379. >array([2, 1, 1], 'l')
  380.  
  381. divmod(array([2,1]), array([1,2]))
  382. >(array([2, 0], 'l'), array([0, 1], 'l'))
  383.  
  384. 4l*arange(1,3)
  385. >array([4L, 8L], 'O')
  386.  
  387. arange(3L)
  388. >array([0L, 1L, 2L], 'O')
  389.  
  390. import sys
  391. from Numeric import *
  392.  
  393. #Creation from a nested sequence
  394. array([1,2,3])
  395. >array([1, 2, 3], 'l')
  396.  
  397. array([1,2.3,4])
  398. >array([1.0, 2.3, 4.0], 'd')
  399.  
  400. array([1,2j,3.])
  401. >array([(1+0j), 2j, (3+0j)], 'D')
  402.  
  403. array([1,2,3], Float32)
  404. >array([1.0, 2.0, 3.0], 'f')
  405.  
  406. array([[1,2],[11,12]])
  407. >array([[1, 2], [11, 12]], 'l')
  408.  
  409. array([[],[]]).shape
  410. >(2, 0)
  411.  
  412. array([])
  413. >array([], 'l')
  414.  
  415. #These are not just convenience functions
  416. zeros( (2,3) )
  417. >array([[0, 0, 0], [0, 0, 0]], 'l')
  418.  
  419. ones( (2,3), Int16)
  420. >array([[1, 1, 1], [1, 1, 1]], 's')
  421.  
  422. #arrayrange now
  423. arrayrange(7, step=2)
  424. >array([0, 2, 4, 6], 'l')
  425.  
  426. arrayrange(0, 1.5, .5)
  427. >array([0.0, 0.5, 1.0], 'd')
  428.  
  429. #Creation from String Data
  430. fromstring("\001\002\003", 'b')
  431. >array([1, 2, 3], 'b')
  432.  
  433.  
  434. #Structural array operations
  435. m = array([[1,2,3], [11,12,13]])
  436. a = arange(6)
  437.  
  438. a[2:-1]
  439. >array([2, 3, 4], 'l')
  440.  
  441. a[::-1]
  442. >array([5, 4, 3, 2, 1, 0], 'l')
  443.  
  444.  
  445. m[0,2]
  446. >3
  447.  
  448. m[...,1]
  449. >array([2, 12], 'l')
  450.  
  451. #take
  452.  
  453. take(a, (3,2,1,2,3))
  454. >array([3, 2, 1, 2, 3], 'l')
  455.  
  456. take(m, (2,2,1,1), -1)
  457. >array([[3, 3, 2, 2], [13, 13, 12, 12]], 'l')
  458.  
  459. #Rearranging array elements
  460.  
  461. reshape(m, (-1,2))
  462. >array([[1, 2], [3, 11], [12, 13]], 'l')
  463.  
  464. reshape(a, (2,3))
  465. >array([[0, 1, 2], [3, 4, 5]], 'l')
  466.  
  467. reshape(a, (4,-1))
  468. >exceptions.ValueError: total size of new array must be unchanged
  469.  
  470. reshape(a, (5,6))
  471. >exceptions.ValueError: total size of new array must be unchanged
  472.  
  473. ravel(m)
  474. >array([1, 2, 3, 11, 12, 13], 'l')
  475.  
  476. a[..., NewAxis]
  477. >array([[0], [1], [2], [3], [4], [5]], 'l')
  478.  
  479. transpose(m, (1,0))
  480. >array([[1, 11], [2, 12], [3, 13]], 'l')
  481.  
  482. #Replicating and combining array elements
  483.  
  484. repeat(a, (0,1,2,3,0,0))
  485. >array([1, 2, 2, 3, 3, 3], 'l')
  486.  
  487. repeat(a, (0,)*6)
  488. >array([], 'l')
  489.  
  490. repeat(m, (0,2,1), -1)
  491. >array([[2, 2, 3], [12, 12, 13]], 'l')
  492.  
  493. #al = [0,1,2,3,4,5]
  494. concatenate( (a[:3], a[3:]) )
  495. >array([0, 1, 2, 3, 4, 5], 'l')
  496.  
  497. concatenate( (m, m) )
  498. >array([[1, 2, 3], [11, 12, 13], [1, 2, 3], [11, 12, 13]], 'l')
  499.  
  500. concatenate( (m, m), axis=1)
  501. >array([[1, 2, 3, 1, 2, 3], [11, 12, 13, 11, 12, 13]], 'l')
  502.  
  503. #arithmetic and logic operators
  504. x = array([1,2], 'l')
  505. y = zeros((2,), 'l')
  506.  
  507. x+x
  508. >array([2, 4], 'l')
  509.  
  510. add(x,x)
  511. >array([2, 4], 'l')
  512.  
  513. add(x,x,y)
  514. >array([2, 4], 'l')
  515.  
  516. y
  517. >array([2, 4], 'l')
  518.  
  519. x-x
  520. >array([0, 0], 'l')
  521.  
  522. subtract(x,x)
  523. >array([0, 0], 'l')
  524.  
  525. x*x
  526. >array([1, 4], 'l')
  527.  
  528. multiply(x, x)
  529. >array([1, 4], 'l')
  530.  
  531. x/x
  532. >array([1, 1], 'l')
  533.  
  534. divide(x,x)
  535. >array([1, 1], 'l')
  536.  
  537. x**x
  538. >array([1, 4], 'l')
  539.  
  540. power(x,x)
  541. >array([1, 4], 'l')
  542.  
  543. x%x
  544. >array([0, 0], 'l')
  545.  
  546. remainder(x,x)
  547. >array([0, 0], 'l')
  548.  
  549. #Need more math functions here...
  550.  
  551. #Non-elementwise operations
  552. add.reduce(a)
  553. >15
  554.  
  555. add.reduce(m, 0)
  556. >array([12, 14, 16], 'l')
  557.  
  558. add.reduce(m, -1)
  559. >array([6, 36], 'l')
  560.  
  561. add.reduce([1])
  562. >1
  563.  
  564. add.reduce([])
  565. >0
  566.  
  567. multiply.reduce([])
  568. >1
  569.  
  570. add.outer(a[:3], a[:3])
  571. >array([[0, 1, 2], [1, 2, 3], [2, 3, 4]], 'l')
  572.  
  573. dot(a,a)
  574. >55
  575.  
  576. #conditional selection
  577.  
  578. choose(a, (5,4,3,2,1,0))
  579. >array([5, 4, 3, 2, 1, 0], 'l')
  580.  
  581. choose([[1,0], [0,1]], (66, [(1,2),(11,12)]))
  582. >array([[1, 66], [66, 12]], 'l')
  583.  
  584. #sorting
  585.  
  586. s = (3,2,5,1,4,0)
  587. sm = [s, array(s)[::-1]]
  588.  
  589. sort( s )
  590. >array([0, 1, 2, 3, 4, 5], 'l')
  591.  
  592. argsort( s )
  593. >array([5, 3, 1, 0, 4, 2], 'l')
  594.  
  595. sort(sm, axis=-1)
  596. >array([[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]], 'l')
  597.  
  598. sort(sm, axis=0)
  599. >array([[0, 2, 1, 1, 2, 0], [3, 4, 5, 5, 4, 3]], 'l')
  600.  
  601. searchsorted( arange(10), (5, 2))
  602. >array([5, 2], 'l')
  603.  
  604. #argmax/min
  605.  
  606. argmax( s )
  607. >2
  608.  
  609. argmax( sm, axis=-1 )
  610. >array([2, 3], 'l')
  611.  
  612. argmax(sm, axis=0)
  613. >array([0, 1, 0, 1, 0, 1], 'l')
  614.  
  615. argmin(sm, axis=-1)
  616. >array([5, 0], 'l')
  617.  
  618. #Implementation methods
  619.  
  620. array([3,4], Int32).itemsize()
  621. >4
  622.  
  623. a.astype('i').byteswapped()
  624. >array([0, 16777216, 33554432, 50331648, 67108864, 83886080], 'i')
  625.  
  626. a.typecode()
  627. >l
  628.  
  629. a.iscontiguous()
  630. >1
  631.  
  632. a.astype('d')
  633. >array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0], 'd')
  634.  
  635. u = array([0,1,2], Int8)
  636. repr(u.tostring())
  637. >'\000\001\002'
  638.  
  639. c = array([0,1,1j])
  640. c.real
  641. >array([0.0, 1.0, 0.0], 'd')
  642.  
  643. c.imag
  644. >array([0.0, 0.0, 1.0], 'd')
  645.  
  646. #init = "from multiarray import *\nm = array([(5,2,6,9,3,4,1,0), (8,0,1,3,6,2,0,4)])"
  647. #sorts = [("sort(m)", array([(0,1,2,3,4,5,6,9),(0,0,1,2,3,4,6,8)], 'l') ),
  648. #    ("argmax(m)[1]", array([3,0], 'l') ),
  649. #    ("argmin(m)[1]", array([7,1], 'l') ),
  650. #    ]
  651. #do_eval(sorts, initialize = init)
  652.  
  653.  
  654.  
  655. #sort and argsort need axis
  656.  
  657. a = arange(6)
  658.  
  659. #from Guido
  660. a[...]
  661. >array([0, 1, 2, 3, 4, 5], 'l')
  662.  
  663. array([None])
  664. >array([None], 'O')
  665.  
  666. zeros( (2,), 'O')
  667. >array([0, 0], 'O')
  668.  
  669. #from Janne
  670. b = array(a)
  671. b[0] = 66
  672. a[0]
  673. >0
  674.  
  675. zeros((-5,))
  676. >exceptions.ValueError: negative dimensions are not allowed
  677.  
  678. arange(5,3,2)
  679. >array([], 'l')
  680.  
  681. minimum.reduce()
  682. >exceptions.TypeError: function requires at least 1 argument; 0 given
  683.  
  684. minimum.reduceat()
  685. >exceptions.TypeError: function requires exactly 2 arguments; 0 given
  686.  
  687. b = array([[1, 2, 3, 4], [5, 6, 7, 8]]*2)
  688.  
  689. diagonal(b)
  690. >array([1, 6, 3, 8], 'l')
  691.  
  692. diagonal(b, -1)
  693. >array([5, 2, 7], 'l')
  694.  
  695. c = array([b,b])
  696.  
  697. diagonal(c, 1)
  698. >array([[2, 7, 4], [2, 7, 4]], 'l')
  699.  
  700. #From Carlos
  701. b[1:1].shape
  702. >(0, 4)
  703.  
  704. b[1:1,:].shape
  705. >(0, 4)
  706.  
  707. b[10:]
  708. >zeros((0,4))
  709.  
  710. b[10:, :]
  711. >zeros((0,4))
  712.  
  713. b[2:10]
  714. >array([[1, 2, 3, 4], [5, 6, 7, 8]], 'l')
  715.  
  716. b[2:10, ...]
  717. >array([[1, 2, 3, 4], [5, 6, 7, 8]], 'l')
  718.  
  719. 1/array(0)
  720. >exceptions.ZeroDivisionError: divide by zero
  721.  
  722. #1/array(0.)
  723. #Machine dependent value, just hope there's no system crash on this
  724. #>1.#INF
  725.  
  726. #Tim Hochberg
  727.  
  728. choose((0,1,2),([1,1,1],[2,2,2]))
  729. >exceptions.ValueError: invalid entry in choice array
  730.  
  731. where((0,1,2), [1,1,1],[2,2,2])
  732. >array([2, 1, 1], 'l')
  733.  
  734. divmod(array([2,1]), array([1,2]))
  735. >(array([2, 0], 'l'), array([0, 1], 'l'))
  736.  
  737. 4l*arange(1,3)
  738. >array([4L, 8L], 'O')
  739.  
  740. arange(3L)
  741. >array([0L, 1L, 2L], 'O')
  742.  
  743. # Test of the exception-raising comparisons
  744. # david ascher - march 16, 1998
  745. x = array([1,2])
  746. y = array([1,2])
  747. z = array([1,2,3])
  748. x < y
  749. >exceptions.TypeError: Comparison of multiarray objects is not implemented.
  750. x == z
  751. >exceptions.TypeError: Comparison of multiarray objects is not implemented.
  752. cmp(y,z)
  753. >exceptions.TypeError: Comparison of multiarray objects is not implemented.
  754. # Test of fromfunction
  755. fromfunction(add, (2,2))
  756. >array([[0, 1],[1, 2]])
  757.